<cfset var.start = CreateODBCdateTime(now())>

<cfsetting enablecfoutputonly="yes">
<cfparam name="attributes.pass_cart" type="any" default="1">
<cfparam name="attributes.url" default="#application.xtiurl#">
<cfparam name="attributes.dtd" default="#application.xtidtd#">
<cfparam name="attributes.debug" default="false">
<cfparam name="attributes.debuglevel" default="1,2,3">
<cfparam name="attributes.customerid">
<cfparam name="attributes.contactid" default="">
<cfparam name="attributes.crt_order" default="">
<cfparam name="attributes.order_number" default="">

<!--- ORDER HEADER --->
<cfparam name="attributes.misc_code_1" default="">
<cfparam name="attributes.misc_amt_1" default="">
<cfparam name="attributes.misc_code_2" default="">
<cfparam name="attributes.misc_amt_2" default="">
<cfparam name="attributes.source">
<cfparam name="attributes.ship_complete" default="">
<cfparam name="attributes.tsr_taker">
<cfparam name="attributes.user" default="web">
<cfparam name="attributes.locid">
<cfparam name="attributes.cid" default="">
<cfparam name="attributes.client_order" default="">
<cfparam name="attributes.order_type" default="R">
<cfparam name="attributes.program_id" default="">
<cfparam name="attributes.gsa_contract_number" default="">
<!--- GSA project --->
<cfif len(trim(attributes.gsa_contract_number))>
	<cfset attributes.program_id = attributes.gsa_contract_number>
</cfif>
<cfparam name="attributes.confidence" default="">
<cfparam name="attributes.close_date" default="">
<cfparam name="attributes.close_amt" default="">

<!--- Heavyweight shipping service flags --->
<cfparam name="attributes.inside_delivery" default="N">
<cfparam name="attributes.lift_gate" default="N">
<cfparam name="attributes.pallet_remove" default="N">
<cfparam name="attributes.biddesk_flag" default="">
<cfif len(attributes.biddesk_flag)>
	<cfset var.biddesk_flag = "<biddesk_flag>" & ucase(attributes.biddesk_flag) & "</biddesk_flag>">
<cfelse>
	<cfset var.biddesk_flag = "">
</cfif>
<!--- P03285 Cybersource --->
<cfparam name="attributes.cus_browser_email" default=""><!--- placeholder only --->
<cfparam name="attributes.cus_cookies_accepted" default="Y"><!--- always true --->
<cfparam name="attributes.cus_ip_address" default=""><!--- placeholder only --->
<cfparam name="attributes.cus_email" default="">
<cfparam name="attributes.cus_hostname" default=""><!--- placeholder only --->
<cfparam name="attributes.cus_browser_type" default="#cgi.http_user_agent#">
<cfparam name="attributes.last_four_ssn" type="any" default="">
<cfparam name="attributes.date_of_birth" type="any" default="">
<cfparam name="attributes.promotional_financing" type="any" default="">
<cfparam name="attributes.order_origin" type="any" default="">

<!--- parse out to only pass in value before the "(" 
<cfif isdefined("cgi.http_user_agent") and len(cgi.http_user_agent)>
	<cfset attributes.cus_browser_type=trim(left(cgi.http_user_agent, find("(",cgi.http_user_agent) - 1))>
</cfif>--->
<!--- P03285 end --->

<!--- PAYMENT TYPE --->
<!--- P2998  replace seperate credit card attributes with an array :array_ccs_new/array_ccs_orig --->
<cfparam name="attributes.payment_type" default="Credit_Card">
<cfparam name="attributes.pay_type" default="V">
<cfparam name="attributes.array_ccs_new" default="">
<cfparam name="attributes.array_ccs_orig" default="">
<cfparam name="attributes.ponumber" default="">

<!--- BILLING PARAMS --->
<cfparam name="attributes.b_attention" default="">

<!--- SHIPPING PARAMS --->
<cfparam name="attributes.company_name">
<cfparam name="attributes.attention">
<cfparam name="attributes.address1">
<cfparam name="attributes.address2" default="">
<cfparam name="attributes.address3" default="">
<cfparam name="attributes.address4" default="">
<cfparam name="attributes.city">
<cfparam name="attributes.state">
<cfparam name="attributes.country">
<cfparam name="attributes.zip" default="">
<cfparam name="attributes.geo_code" default="">
<cfparam name="attributes.phone" default="">
<!---SHIPPING CODES  --->
<cfparam name="attributes.code">
<cfparam name="attributes.account_number" default="">
<cfparam name="attributes.ship_price" default="">
<cfparam name="attributes.ship_code_reason" default="">
<cfparam name="attributes.cqsd" default="">
<cfparam name="attributes.tax_exemption_requested" default="">
<!--- SYSTEM NOTES --->
<cfparam name="attributes.packing_slip_notes" default="">
<cfparam name="attributes.invoice_notes" default="">
<cfparam name="attributes.management_notes" default="">
<cfparam name="attributes.auto_bo" default="">
<cfparam name="attributes.future_ship" default="">
<cfparam name="attributes.ibm_cust_num" default="">
<cfparam name="attributes.ibm_tier_code" default="">
<!--- ARRAYS SYSTEM AND LINITEM  --->
<cfparam name="attributes.array_parts">
<cfparam name="attributes.array_systems" default="">
<cfparam name="attributes.array_warranty" default="">


<cfif isdefined("session.customer_type") and listfindnocase("H,HE",trim(session.customer_type))>
	<cfset attributes.contactid = "">
</cfif>

<!--- 
modified per Incident #1016626 stripping invalid chars in program ID 
Valid Inputs:
A-Z a-Z 0-9 \ / - " " (spaces)
--->
<cfset attributes.program_id = REReplace(attributes.program_id,"[^0-9a-zA-Z\s\\/-]","","ALL")>

<!--- Modified per project p 4564 --->
<!--- per project 4564 --->
<cfif isdefined("request.dsn") and findnocase("oe",request.dsn)>
	<cfset service_pac_list = "">
	<cfquery name="qServiceOfferingGet" datasource="#request.dsn#">
		asp_service_offering_type_get
	</cfquery>
	
	<cfloop query="qServiceOfferingGet">
		<cfquery name="qServiceOfferingPartGet" datasource="#request.dsn#">
			asp_service_offering_get @iservice_offering_id = '#qServiceOfferingGet.service_offering_id #'
		</cfquery>
		<cfset service_pac_list = ListAppend(service_pac_list, ValueList(qServiceOfferingPartGet.product_code_id, ","))>
	</cfloop>	
</cfif>
<!--- End of modification --->

<!--- ---------------------------------
BEGIN TO BUILD THE XML POST --->
<cfscript>
// Create the payment tags

// info for credit cards existing on the orginal order
payment = "";
if  (isarray(attributes.array_ccs_orig) and arraylen(attributes.array_ccs_orig) GT 0) {
	indexcnt_orig = arraylen(attributes.array_ccs_orig) + 1;
	for ( i=1; i lt indexcnt_orig ; i=i+1) {
		payment = payment & '		 
			<payment type="#attributes.payment_type#">
			<ccln>#ucase(attributes.array_ccs_orig[i].cc_line)#</ccln>
			<partial_ccn>#ucase(attributes.array_ccs_orig[i].partial_num)#</partial_ccn>
			<amount>#ucase(attributes.array_ccs_orig[i].amount)#</amount>
			<expr_date>#ucase(attributes.array_ccs_orig[i].expiration)#</expr_date>
			<po_number>#ucase(attributes.ponumber)#</po_number>
		</payment>';
	}
}
/*
else if (len(attributes.payment_type) GT 0){
	payment = '
	<payment type="#attributes.payment_type#">
		<ccln></ccln>
		<partial_ccn></partial_ccn>
		<amount></amount>
		<expr_date></expr_date>
		<po_number>#ucase(attributes.ponumber)#</po_number>
	</payment>';
}
*/

// info for credit cards added on the post
new_payment="";
if  (isarray(attributes.array_ccs_new) and arraylen(attributes.array_ccs_new) GT 0) {
	indexcnt_new = arraylen(attributes.array_ccs_new) + 1;
	for ( j=1; j lt indexcnt_new ; j=j+1) {
	  	new_payment = new_payment & '
		<payment type="#attributes.payment_type#">
			<ccln>#ucase(attributes.array_ccs_new[j].cc_line)#</ccln>
			<card_number>#ucase(attributes.array_ccs_new[j].cc_num)#</card_number>
			<expr_date>#ucase(attributes.array_ccs_new[j].cc_exp)#</expr_date>
			<amex_num>#ucase(attributes.array_ccs_new[j].amex_num)#</amex_num>
			<po_number>#ucase(attributes.ponumber)#</po_number>
			<amount>#Replace(attributes.array_ccs_new[j].cc_amt, ',', '', 'ALL')#</amount>
			<security_code>#ucase(attributes.array_ccs_new[j].cc_security)#</security_code>
			<last_four_ssn>#attributes.last_four_ssn#</last_four_ssn>
			<date_of_birth>#attributes.date_of_birth#</date_of_birth>
			<promotional_financing>#attributes.promotional_financing#</promotional_financing>
		</payment>		  
	  	';
	}
}
else if (len(attributes.payment_type) GT 0){
	new_payment = '
	<payment type="#attributes.payment_type#">
		<card_number></card_number>
		<expr_date></expr_date>
		<amex_num></amex_num>
		<po_number>#ucase(attributes.ponumber)#</po_number>
		<amount></amount>
		<security_code></security_code>
		<last_four_ssn>#attributes.last_four_ssn#</last_four_ssn>
		<date_of_birth>#attributes.date_of_birth#</date_of_birth>
		<promotional_financing>#attributes.promotional_financing#</promotional_financing>
	</payment>';
}
</cfscript>

<!--- Standalone parts --->

<cfscript>
cart = "";
subtotal = 0;
warranty_block = "";
service_pac = "";
</cfscript>

<cfif (isarray(attributes.array_parts) and arraylen(attributes.array_parts) GT 0)>
<cfset indexcnt = arraylen(attributes.array_parts) + 1>
	<cfloop index="i" from="1" to="#arraylen(attributes.array_parts)#">
			
			<!--- start warranty info --->
			<cfif IsDefined("request.warranty_present") AND request.warranty_present EQ 'true' AND IsDefined("request.get_warranty_records.cart_line_item_id")>
				<cfquery name="find_warranty" dbtype="query">
				select *
				from request.get_warranty_records
				where cart_line_item_id=#attributes.array_parts[i].line_counter#
				</cfquery>
				
				<cfif find_warranty.recordcount>
				<cfoutput query="find_warranty">
				<cfscript>
				warranty_block = warranty_block & '
				<warranty>
				  <warranty_serialnum>#find_warranty.serial_num#</warranty_serialnum>
				  <warranty_model>#find_warranty.machine_model_num#</warranty_model>
				  <warranty_purchase_date>#dateformat(find_warranty.purchase_date, 'mm/dd/yy')#</warranty_purchase_date>
				  <status>#find_warranty.verified_flag#</status>
				  <associated>#find_warranty.associated_flag#</associated>
				</warranty>';
				</cfscript>
				</cfoutput>
				</cfif>
				
				<!--- Modified per project p 4564 --->
				<cfif IsDefined("session.after_market_service_pac") and session.after_market_service_pac eq "Y" and ListFindNoCase(service_pac_list, attributes.array_parts[i].product)>
					<cfset service_pac = "Y">
					<cfset rc = StructDelete(session, "after_market_service_pac", "True")>
				</cfif>
				<!--- End of modification --->
			</cfif>
			<!--- end warranty info --->
	  
	  <cfscript>
	  /* <configurationID>#ucase(attributes.array_parts[i].iss_configuration_id)#</configurationID> == removed per Blue Horizon replacement bug #2363 */ 
	  cart = cart & '
		<line_item>
			<product>				
				<product_code>#ucase(attributes.array_parts[i].product)#</product_code>';
				if (attributes.array_parts[i].type IS "original"){
				cart = cart & '
				<ln>#ucase(attributes.array_parts[i].line)#</ln>';
				}
				cart = cart & '
				<configurationID></configurationID>
				<quantity>#ucase(attributes.array_parts[i].quantity)#</quantity>
				<price>#ucase(attributes.array_parts[i].price)#</price>
				<list_price>#ucase(attributes.array_parts[i].list_price)#</list_price>
				<green>#ucase(attributes.array_parts[i].green_price)#</green>
				<yellow>#ucase(attributes.array_parts[i].yellow_price)#</yellow>
				<bmc_cost>#ucase(attributes.array_parts[i].iss_bmc_cost)#</bmc_cost>
				<floor_price>#ucase(attributes.array_parts[i].floor_price)#</floor_price>
				<promo_code_1>#ucase(attributes.array_parts[i].promo_code_1)#</promo_code_1>
				<promo_code_2>#ucase(attributes.array_parts[i].promo_code_2)#</promo_code_2>
				<service_pac>#service_pac#</service_pac>
				<rsn_code_prc>#ucase(attributes.array_parts[i].price_reason)#</rsn_code_prc>
				<bid>#ucase(attributes.array_parts[i].bid)#</bid>
				<bid_exp>#ucase(attributes.array_parts[i].bid_exp)#</bid_exp>
				<sub_line>#ucase(attributes.array_parts[i].sub_line)#</sub_line>
				<warranty_list>#warranty_block#</warranty_list>
				<is_accesory_of>#attributes.array_parts[i].is_accessory_of#</is_accesory_of>
				<accessorized_line>#attributes.array_parts[i].accessorized_line#</accessorized_line>
			</product>
		</line_item>';
	  warranty_block = "";
	  </cfscript>
	  
	</cfloop>
</cfif>


<cfscript>
// Now the systems 
if  (isarray(attributes.array_systems) and arraylen(attributes.array_systems)) {
	indexcnt1 = arraylen(attributes.array_systems) + 1;  
 //writeoutput(arraylen(attributes.array_systems));
	for (x=1; x lt indexcnt1; x=x+1) {
	  cart = cart & '
		<line_item>
			<configuration>';
				if (attributes.array_systems[x].type IS "original"){
				cart = cart & '
				<ln>#ucase(attributes.array_systems[x].line)#</ln>';
				}
				cart = cart & '
				<configurationID>#ucase(attributes.array_systems[x].iss_configuration_id)#</configurationID>
				<quantity>#ucase(attributes.array_systems[x].quantity)#</quantity>
				<baseunit>
					<product_code>#ucase(attributes.array_systems[x].b_product)#</product_code>
					<price>#ucase(attributes.array_systems[x].price)#</price>
					<list_price>#ucase(attributes.array_systems[x].b_list_price)#</list_price>
					<green>#ucase(attributes.array_systems[x].b_green_price)#</green>
					<yellow>#ucase(attributes.array_systems[x].b_yellow_price)#</yellow>
					<bmc_cost>#ucase(attributes.array_systems[x].iss_bmc_cost)#</bmc_cost>
					<floor_price>#ucase(attributes.array_systems[x].b_floor_price)#</floor_price>
					<promo_code_1>#ucase(attributes.array_systems[x].promo_code_1)#</promo_code_1>
					<promo_code_2>#ucase(attributes.array_systems[x].promo_code_2)#</promo_code_2>
					<rsn_code_prc>#ucase(attributes.array_systems[x].price_reason)#</rsn_code_prc>
					<bid>#ucase(attributes.array_systems[x].bid)#</bid>
					<bid_exp>#ucase(attributes.array_systems[x].bid_exp)#</bid_exp>
					<sub_line>#ucase(attributes.array_systems[x].sub_line)#</sub_line>
					<is_accesory_of>#attributes.array_systems[x].is_accessory_of#</is_accesory_of>
					<accessorized_line>#attributes.array_systems[x].accessorized_line#</accessorized_line>
				</baseunit>';
		indexcnt2 = arraylen(attributes.array_systems[x].subitem) + 1;
		//writeoutput(arraylen(attributes.array_systems[x].subitem));
		for (y=1; y lt indexcnt2;  y=y+1) {
		cart = cart & '
				<component>';
					cart = cart & '
					<product_code>#ucase(attributes.array_systems[x].subitem[y].product)#</product_code>
					<quantity>#ucase(attributes.array_systems[x].subitem[y].quantity)#</quantity>
					<price>#ucase(attributes.array_systems[x].subitem[y].price)#</price>
					<rsn_code_prc>#ucase(attributes.array_systems[x].subitem[y].price_reason)#</rsn_code_prc>
					<product_characteristic>#ucase(attributes.array_systems[x].subitem[y].iss_product_char)#</product_characteristic>
					<product_characteristic_value>#ucase(attributes.array_systems[x].subitem[y].iss_product_value)#</product_characteristic_value>
					<product_quantity_characteristic>#ucase(attributes.array_systems[x].subitem[y].iss_qty_char)#</product_quantity_characteristic>
					<product_quantity_characteristic_value>#ucase(attributes.array_systems[x].subitem[y].iss_qty_value)#</product_quantity_characteristic_value>
				</component>';
		}
		cart = cart & '
			</configuration>
		</line_item>';
		 }	
//	}
} 

// clear cart data if pass_cart EQ 0 
if (IsDefined("attributes.pass_cart") AND NOT attributes.pass_cart) {
	cart='';
	shopping_cart='';
}
else {
	shopping_cart='<shopping_cart>
			<issCartID></issCartID>
			#cart#
		</shopping_cart>';
}

// Now build the rest of the xml to post to middle ware. KEEP FORMATING SAME AS BELOW...
xml = '<!DOCTYPE copy SYSTEM "#attributes.dtd#">
<xtiinput>
	<inputheader>
		<actionset>
			<action>chg_order</action>
		</actionset>
	</inputheader>
	<inputdata>
		<customerid>#ucase(attributes.customerid)#</customerid>
		<contactid>#ucase(attributes.contactid)#</contactid>
		<order_number>#ucase(attributes.order_number)#</order_number>
		<ibm_cust_num>#ucase(attributes.ibm_cust_num)#</ibm_cust_num>
		<ibm_tier_code>#ucase(attributes.ibm_tier_code)#</ibm_tier_code>
		<order_header>
			#var.biddesk_flag#
			<misc_code_1>#ucase(attributes.misc_code_1)#</misc_code_1>
			<misc_amt_1>#ucase(attributes.misc_amt_1)#</misc_amt_1>
			<misc_code_2>#ucase(attributes.misc_code_2)#</misc_code_2>
			<misc_amt_2>#ucase(attributes.misc_amt_2)#</misc_amt_2>
			<source>#ucase(attributes.source)#</source>
			<ship_complete>#ucase(attributes.ship_complete)#</ship_complete>
			<tsr_taker>#ucase(attributes.tsr_taker)#</tsr_taker>
			<user>#ucase(attributes.user)#</user>
			<client_order>#ucase(attributes.client_order)#</client_order>
			<order_type>#xmlformat(ucase(attributes.order_type))#</order_type>
			<program_id>#attributes.program_id#</program_id>
			<locid>#attributes.locid#</locid>
			<cid>#attributes.cid#</cid>
			<pay_type>#attributes.pay_type#</pay_type>
			<order_origin>#attributes.order_origin#</order_origin>
			<confidence>#attributes.confidence#</confidence>
			<close_date>#attributes.close_date#</close_date>
			<close_amt>#attributes.close_amt#</close_amt>
			<auto_bo>#ucase(attributes.auto_bo)#</auto_bo>
			<future_ship_date>#ucase(attributes.future_ship)#</future_ship_date>
			<cqsd>#ucase(attributes.cqsd)#</cqsd>
			<inside_delivery>#attributes.inside_delivery#</inside_delivery>
			<lift_gate>#attributes.lift_gate#</lift_gate>
			<pallet_remove>#attributes.pallet_remove#</pallet_remove>
			<cus_browser_email>#attributes.cus_browser_email#</cus_browser_email>
			<cus_browser_type>#attributes.cus_browser_type#</cus_browser_type>
			<cus_cookies_accepted>#attributes.cus_cookies_accepted#</cus_cookies_accepted>
			<cus_email>#attributes.cus_email#</cus_email>
			<cus_hostname>#attributes.cus_hostname#</cus_hostname>
			<cus_ip_address>#attributes.cus_ip_address#</cus_ip_address>
			<tax_exemption_requested>#ucase(attributes.tax_exemption_requested)#</tax_exemption_requested>
		</order_header>
		#payment#
		#new_payment#
		<billing_addr>
			<attention>#replace(xmlformat(ucase(attributes.b_attention)), '&amp;AMP;', '&amp;', 'all')#</attention>
		</billing_addr>
		<shipping_addr>
			<company_name>#replace(xmlformat(ucase(attributes.company_name)), '&amp;AMP;', '&amp;', 'all')#</company_name>
			<attention>#replace(xmlformat(ucase(attributes.attention)), '&amp;AMP;', '&amp;', 'all')#</attention>
			<address_1>#replace(xmlformat(ucase(attributes.address1)), '&amp;AMP;', '&amp;', 'all')#</address_1>
			<address_2>#replace(xmlformat(ucase(attributes.address2)), '&amp;AMP;', '&amp;', 'all')#</address_2>
			<city>#ucase(attributes.city)#</city>
			<state>#ucase(attributes.state)#</state>
			<country>#ucase(attributes.country)#</country>
			<zip>#ucase(attributes.zip)#</zip>
			<geo_code>#attributes.geo_code#</geo_code>
			<phone>#ucase(attributes.phone)#</phone>
		</shipping_addr>
		<shipping_codes>
			<method>
				<code>#ucase(attributes.code)#</code>
				<account_number>#ucase(attributes.account_number)#</account_number>
				<price>#ucase(attributes.ship_price)#</price>
				<rsn_code_ship>#ucase(trim(attributes.ship_code_reason))#</rsn_code_ship>
			</method>
		</shipping_codes>
		<system_notes>
			<packing_slip_notes>#xmlformat(ucase(attributes.packing_slip_notes))#</packing_slip_notes>
			<invoice_notes>#xmlformat(ucase(attributes.invoice_notes))#</invoice_notes>
			<management_notes>#xmlformat(ucase(attributes.management_notes))#</management_notes>
		</system_notes>
		#shopping_cart#
	</inputdata>
</xtiinput>';
</cfscript>